package com.ruoyi.file.utils;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * The bdap file content util. funciton list: 1. read a txt file a ArrayList. 2.
 * get the content of file(pdf, word, txt and so on). 3. write/append a a file.
 *
 * @author william
 * @since jkd1.8.0_144
 * @version 2019.11.4
 *
 */
public class VaipFileContentUtil {

	private final Logger logger = LoggerFactory.getLogger(VaipFileContentUtil.class);

	public VaipFileContentUtil() {

	}

	public boolean isTextFile(String fileName) {
		VaipFileUtil fileUtil = new VaipFileUtil();
		return fileUtil.isTextFile(fileName);
	}

	public boolean isVideoFile(String fileName) {
		VaipFileUtil fileUtil = new VaipFileUtil();
		return fileUtil.isVideoFile(fileName);
	}

	public boolean isAudioFile(String fileName) {
		VaipFileUtil fileUtil = new VaipFileUtil();
		return fileUtil.isAudioFile(fileName);
	}

	public boolean isPicture(String fileName) {
		VaipFileUtil fileUtil = new VaipFileUtil();
		return fileUtil.isPicture(fileName);
	}

	/**
	 * get the list of a file. read the file as line and add to list.
	 *
	 * @param filepath
	 * @return the list of file line.
	 * @throws Exception
	 * @throws UnsupportedEncodingException
	 */
	public List<String> readFileToList(String filepath, String coding, boolean format) throws Exception {
		List<String> contentList = new ArrayList<String>();
		InputStreamReader isr = new InputStreamReader(new FileInputStream(filepath), coding);
		BufferedReader bf = new BufferedReader(isr);

		String content = "";
		while (content != null) {
			content = bf.readLine();
			if (content == null) {
				break;
			}
			if (format) {
				content = replaceBlank(content);
			}
			contentList.add(content.trim());
		}
		bf.close();
		return contentList;

	}

	private static Pattern blankPattern = Pattern.compile("\\s*|\t|\r|\n");

	static String replaceBlank(String str) {
		String dest = "";
		if (str != null) {
			Matcher m = blankPattern.matcher(str);
			dest = m.replaceAll("");
		}
		return dest;
	}

	/**
	 * 获取全部文件路径。
	 *
	 * @param path
	 * @return
	 */
	public List<File> getFilesUnderDir(String path) {
		File root = new File(path);
		List<File> files = new ArrayList<File>();
		if (!root.isDirectory()) {
			files.add(root);
		} else {
			File[] subFiles = root.listFiles();
			for (File f : subFiles) {
				files.addAll(getFilesUnderDir(f.getAbsolutePath()));
			}
		}
		return files;
	}

	public void appendStringToTxtFile(String filePath, String fileName, String content) throws Exception {
		File dir = new File(filePath);
		if (!dir.exists()) {
			dir.mkdirs();
		}
		File file = new File(filePath + "/" + fileName);
		// file.delete();
		// file.createNewFile();
		BufferedWriter output = new BufferedWriter(new FileWriter(file, true));

		output.write(content);
		output.write("\r\n");
		output.flush();
		output.close();
	}

	/**
	 * write a string to a txt file.
	 *
	 * @param filePath the file path.
	 * @param fileName the name of file.
	 * @param content  the content need to write to file.
	 * @param coding   the coding of file.
	 * @throws Exception if any exceptions happened, throw them.
	 */
	public void writeStringToTxtFile(String filePath, String fileName, String content, String coding) throws Exception {
		File dir = new File(filePath);
		if (!dir.exists()) {
			dir.mkdirs();
		}
		File txt = new File(filePath + "/" + fileName);
		txt.delete();
		txt.createNewFile();

		FileOutputStream fos = new FileOutputStream(txt);
		OutputStreamWriter oStreamWriter = new OutputStreamWriter(new FileOutputStream(txt), coding);
		oStreamWriter.append(content);
		oStreamWriter.close();

		fos.flush();
		fos.close();

	}

	/**
	 * write a string to a txt file.
	 *
	 * @param filePath the file path.
	 * @param content  the content need to write to file.
	 * @param coding   the coding of file
	 * @throws Exception if any exceptions happened throw them.
	 */
	public void writeStringToFile(String filePath, String content, String coding) throws Exception {
		File file = new File(filePath);
		File fileDir = new File(file.getPath());
		if (!fileDir.exists()) {
			fileDir.mkdirs();
		}

		File txt = new File(filePath);
		txt.delete();
		txt.createNewFile();

		FileOutputStream fos = new FileOutputStream(txt);
		OutputStreamWriter oStreamWriter = new OutputStreamWriter(new FileOutputStream(txt), coding);
		oStreamWriter.append(content);
		oStreamWriter.close();

		fos.flush();
		fos.close();
	}

	/**
	 * get the content of txt file.
	 *
	 * @param filePath the txt file path.
	 * @return the content of file.
	 * @throws Exception if any exceptions happened throw them.
	 */
	public String getTxtContent(String filePath) {
		String content = null;
		try {
			if (filePath != null && new File(filePath).exists()) {
				String coding = this.getTxtCoding(filePath);
				// logger.info("-->getTxtContent: " + filePath + ", coding: " + coding);
				content = this.getTxtContent(new FileInputStream(filePath), coding);
			}
		} catch (FileNotFoundException e) {
			logger.error("-->Disposable exception, no break-->");
			logger.error(e.getMessage(), e);
		}

		return content;
	}

	private String getTxtCoding(String txtPath) {
		String charset = "GBK";
		byte[] first3Bytes = new byte[3];
		try {
			boolean checked = false;
			BufferedInputStream bis = new BufferedInputStream(new FileInputStream(txtPath));
			bis.mark(0);
			int read = bis.read(first3Bytes, 0, 3);
			if (read == -1) {
				bis.close();
				// file coding is ANSI.
				return charset;
			} else if (first3Bytes[0] == (byte) 0xFF && first3Bytes[1] == (byte) 0xFE) {
				// file coding is Unicode.
				charset = "UTF-16LE";
				checked = true;
			} else if (first3Bytes[0] == (byte) 0xFE && first3Bytes[1] == (byte) 0xFF) {
				// file coding is Unicode big endian
				charset = "UTF-16BE";
				checked = true;
			} else if (first3Bytes[0] == (byte) 0xEF && first3Bytes[1] == (byte) 0xBB
					&& first3Bytes[2] == (byte) 0xBF) {
				// file coding is UTF-8.
				charset = "UTF-8";
				checked = true;
			}
			bis.reset();
			if (!checked) {
				while ((read = bis.read()) != -1) {
					if (read >= 0xF0)
						// file coding is GBK.
						break;
					if (0x80 <= read && read <= 0xBF)
						// file coding is GBK.
						break;
					if (0xC0 <= read && read <= 0xDF) {
						read = bis.read();
						if (0x80 <= read && read <= 0xBF)
							continue;
						else
							break;
					} else if (0xE0 <= read && read <= 0xEF) {
						read = bis.read();
						if (0x80 <= read && read <= 0xBF) {
							read = bis.read();
							if (0x80 <= read && read <= 0xBF) {
								charset = "UTF-8";
								break;
							} else
								break;
						} else
							break;
					}
				}
			}
			bis.close();
		} catch (Exception e) {
			logger.error("-->Disposable exception, no break-->");
			logger.error(e.getMessage(), e);
		}
		return charset;

	}

	/**
	 * get the content of txt file.
	 *
	 * @param filePath the txt file path.
	 * @return the content of file.
	 * @throws Exception if any exceptions happened throw them.
	 */
	public List<String> getContentList(String filePath) {
		List<String> content = new ArrayList<String>();
		try {
			InputStreamReader isr = new InputStreamReader(new FileInputStream(filePath));
			BufferedReader bf = new BufferedReader(isr);

			String line = "";
			while (line != null) {
				line = bf.readLine();
				if (line == null) {
					break;
				}
				content.add(line);
			}
			bf.close();
			isr.close();

		} catch (Exception e) {
			logger.error("-->Disposable exception, no break-->");
			logger.error(e.getMessage(), e);
		}

		return content;
	}

	/**
	 * get the content of txt file.
	 *
	 * @return the content of file.
	 * @throws Exception if any exceptions happened throw them.
	 */
	public List<String> getContentList(InputStream instream) {
		List<String> content = new ArrayList<String>();
		try {
			InputStreamReader isr = new InputStreamReader(instream);
			BufferedReader bf = new BufferedReader(isr);

			String line = "";
			while (line != null) {
				line = bf.readLine();
				if (line == null) {
					break;
				}
				content.add(line);
			}
			bf.close();
			isr.close();

		} catch (Exception e) {
			logger.error("-->Disposable exception, no break-->");
			logger.error(e.getMessage(), e);
		}

		return content;
	}

	/**
	 * get the content of txt file.
	 *
	 * @return the content of file.
	 * @throws Exception if any exceptions happened throw them.
	 */
	public List<String> getResourceContentList(String resourceName) {
		List<String> content = new ArrayList<>();
		try {
			Resource resource = new ClassPathResource(resourceName);
			content = this.getContentList(resource.getInputStream());
		} catch (IOException e) {
			logger.error("-->Disposable exception, no break-->");
			logger.error(e.getMessage(), e);
		}

		return content;
	}

	public Map<String, String> getResourceContentHashMap(String resourceName, String separator) {
		Map<String, String> content = new HashMap<String, String>();
		try {
			Resource resource = new ClassPathResource(resourceName);
			InputStreamReader isr = new InputStreamReader(resource.getInputStream());
			BufferedReader bf = new BufferedReader(isr);

			String line = "";
			while (line != null) {
				line = bf.readLine();
				if (line == null) {
					break;
				}
				String[] info = line.split(separator);
				if (info.length == 2) {
					content.put(info[0], info[1]);
				}
			}
			bf.close();
			isr.close();
		} catch (Exception e) {
			logger.error("-->Disposable exception, no break-->");
			logger.error(e.getMessage(), e);
		}

		return content;

	}

	/**
	 * get the content of txt file.
	 *
	 * @param instream the file input stream.
	 *
	 * @return the content of file.
	 * @throws Exception if any exceptions happened throw them.
	 */
	public String getTxtContent(InputStream instream) {
		InputStreamReader isr = new InputStreamReader(instream);
		BufferedReader bf = new BufferedReader(isr);

		String content = null;
		try {
			StringBuilder sb = new StringBuilder();
			String line = "";
			while (line != null) {
				line = bf.readLine();
				if (line == null) {
					break;
				}
				sb.append(line + "\n");
			}
			bf.close();
			content = sb.toString();
		} catch (Exception e) {
			logger.error("-->Disposable exception, no break-->");
			logger.error(e.getMessage(), e);
		}

		return content;
	}

	/**
	 * get the content of txt file.
	 *
	 * @param instream the file input stream.
	 *
	 * @return the content of file.
	 * @throws Exception if any exceptions happened throw them.
	 */
	public List<String> getTxtList(InputStream instream) {
		InputStreamReader isr = new InputStreamReader(instream);
		BufferedReader bf = new BufferedReader(isr);

		List<String> content = new ArrayList<String>();
		try {
			String line = "";
			while (line != null) {
				line = bf.readLine();
				if (line == null) {
					break;
				}
				content.add(line);
			}
			bf.close();
		} catch (Exception e) {
			logger.error("-->Disposable exception, no break-->");
			logger.error(e.getMessage(), e);
		}

		return content;
	}

	/**
	 * get the content of txt file.
	 *
	 * @param instream the file input stream.
	 * @param coding   the coding of stream.
	 *
	 * @return the content of file.
	 * @throws Exception if any exceptions happened throw them.
	 */
	public String getTxtContent(InputStream instream, String coding) {
		InputStreamReader isr = null;
		String content = null;
		try {
			isr = new InputStreamReader(instream, coding);
			BufferedReader bf = new BufferedReader(isr);
			StringBuilder sb = new StringBuilder();
			String line = "";
			while (line != null) {
				line = bf.readLine();
				if (line == null) {
					break;
				}
				sb.append(line + "\n");
			}
			bf.close();
			content = sb.toString();
		} catch (Exception e) {
			logger.error("-->Disposable exception, no break-->");
			logger.error(e.getMessage(), e);
		} finally {
			if (null != isr) {
				try {
					isr.close();
				} catch (Exception e) {
					logger.error(e.getMessage(), e);
				}
			}
		}

		return content;
	}

	/**
	 * get the content of txt file.
	 *
	 * @param instream the file input stream.
	 * @param coding   the coding of stream.
	 *
	 * @return the content of file.
	 * @throws Exception if any exceptions happened throw them.
	 */
	public List<String> getTxtList(InputStream instream, String coding) {
		InputStreamReader isr = null;
		List<String> content = new ArrayList<String>();
		try {
			isr = new InputStreamReader(instream, coding);
			BufferedReader bf = new BufferedReader(isr);
			String line = "";
			while (line != null) {
				line = bf.readLine();
				if (line == null) {
					break;
				}
				content.add(line);
			}
			bf.close();
		} catch (Exception e) {
			logger.error("-->Disposable exception, no break-->");
			logger.error(e.getMessage(), e);
		} finally {
			if (null != isr) {
				try {
					isr.close();
				} catch (Exception e) {
					logger.error(e.getMessage(), e);
				}
			}
		}

		return content;
	}

	/**
	 * get the content of txt file.
	 *
	 * @param filePath the txt file path.
	 * @param coding   the coding of file.
	 *
	 * @return the content of file.
	 * @throws Exception if any exceptions happened throw them.
	 */
	public String getTxtContent(String filePath, String coding) {
		String content = null;
		try {
			InputStreamReader isr = new InputStreamReader(new FileInputStream(filePath), coding);
			BufferedReader bf = new BufferedReader(isr);
			StringBuilder sb = new StringBuilder();
			String line = "";
			while (line != null) {
				line = bf.readLine();
				if (line == null) {
					break;
				}
				sb.append(line + "\n");
			}
			bf.close();
			isr.close();
			content = sb.toString();
		} catch (Exception e) {
			logger.error("-->Disposable exception, no break-->");
			logger.error(e.getMessage(), e);
		}

		return content;
	}

	/**
	 * get the content of txt file.
	 *
	 * @param filePath the txt file path.
	 * @param coding   the coding of file.
	 *
	 * @return the content of file.
	 * @throws Exception if any exceptions happened throw them.
	 */
	public List<String> getTxtList(String filePath, String coding) {
		List<String> content = new ArrayList<String>();
		try {
			InputStreamReader isr = new InputStreamReader(new FileInputStream(filePath), coding);
			BufferedReader bf = new BufferedReader(isr);
			String line = "";
			while (line != null) {
				line = bf.readLine();
				if (line == null) {
					break;
				}
				content.add(line.trim());
			}
			bf.close();
			isr.close();
		} catch (Exception e) {
			logger.error("-->Disposable exception, no break-->");
			logger.error(e.getMessage(), e);
		}

		return content;
	}

	/**
	 * get the content of pdf file.
	 *
	 * @param path the path of pdf file.
	 *
	 * @return the pdf content.
	 */
	public String getPDFContent(String path) {

		String result = null;
		try {
			InputStream is = new FileInputStream(path);
			if (is.available() > 0) {
				result = this.getPDFContent(is);
			}
		} catch (Exception e) {
			logger.error("-->Disposable exception, no break-->");
			logger.error(e.getMessage(), e);
		}
		return result;
	}

	/**
	 * get pdf content.
	 *
	 * @param instream the inputstream.
	 * @return
	 */
	public String getPDFContent(InputStream instream) {
		String result = null;
		PDDocument document = null;
		try {
			document = PDDocument.load(instream);
			PDFTextStripper stripper = new PDFTextStripper();
			result = stripper.getText(document).trim();
		} catch (Exception e) {
			logger.error("-->Disposable exception, no break-->");
			logger.error(e.getMessage(), e);
		} finally {
			if (null != document) {
				try {
					document.close();
					instream.close();
				} catch (Exception e) {
					logger.error(e.getMessage(), e);
				}
			}
		}
		return result;
	}

	/**
	 * get the word file content.
	 *
	 * @param filePath the path of file
	 * @return the word content.
	 */
	public String getWordDocContent(String filePath) {
		String content = "";
		try {
			content = getWordDocContent(new FileInputStream(filePath));
		} catch (Exception e) {
			logger.error("error process-->" + filePath);
			logger.error("-->Disposable exception, no break-->");
			logger.error(e.getMessage(), e);
		}
		return content;
	}

	/**
	 * get the word file content.
	 *
	 * @param instream the word input stream.
	 * @return the word content.
	 */
	public String getWordDocContent(InputStream instream) {
		String content = "";
		try {
			WordExtractor re = new WordExtractor(instream);
			content = re.getText();
			re.close();
			instream.close();
		} catch (Exception e) {
			logger.error("-->Disposable exception, no break-->");
			logger.error(e.getMessage(), e);
		}

		return content;
	}

	public String getWordDocxContent(String filePath) {
		String content = "";
		try {
			FileInputStream fis = new FileInputStream(filePath);
			content = this.getWordDocxContent(fis);
		} catch (Exception e) {
			logger.error("-->Disposable exception, no break-->");
			logger.error(e.getMessage(), e);
		}
		return content;
	}

	public String getWordDocxContent(InputStream instream) {
		String content = "";
		try {
			XWPFDocument xdoc = new XWPFDocument(instream);
			XWPFWordExtractor extractor = new XWPFWordExtractor(xdoc);
			content = extractor.getText();
			extractor.close();
			instream.close();
		} catch (Exception e) {
			logger.error("-->Disposable exception, no break-->");
			logger.error(e.getMessage(), e);
		}
		return content;
	}

	public String getXLSContentByJxl(InputStream instream) {
		String content = "";

		/*
		 * Workbook wb = null; try { wb = Workbook.getWorkbook(instream); int sheetSize
		 * = wb.getNumberOfSheets(); StringBuilder cont = new StringBuilder(512); for
		 * (int i = 0; i < sheetSize; i++) { Sheet sheet = wb.getSheet(i); int row_total
		 * = sheet.getRows(); for (int j = 0; j < row_total; j++) { Cell[] cells =
		 * sheet.getRow(j); for (int k = 0; k < cells.length; k++) {
		 * cont.append(cells[k].getContents() + "\n"); } } } content = cont.toString();
		 * } catch (BiffException | Exception e) { e.printStackTrace(); } finally { if
		 * (null != instream) { try { instream.close(); } catch (Exception e) {
		 * e.printStackTrace(); } } }
		 */

		return content;
	}

	public String getXLSContentPoi(InputStream instream, String type) {
		String content = "";
		StringBuilder cont = new StringBuilder(512);
		Workbook workBook = null;
		try {

			if (FILTER_XLS.equals(type)) {
				workBook = new HSSFWorkbook(instream);
			} else if (FILTER_XLSX.equals(type)) {
				workBook = new XSSFWorkbook(instream);
			}

			int sheetNumber = workBook.getNumberOfSheets();
			for (int i = 0; i < sheetNumber; i++) {
				Sheet sheet = workBook.getSheetAt(i);

				int firstRowIndex = sheet.getFirstRowNum() + 1;
				int lastRowIndex = sheet.getLastRowNum();
				for (int rowIndex = firstRowIndex; rowIndex <= lastRowIndex; rowIndex++) {
					Row row = sheet.getRow(rowIndex);
					if (row != null) {
						int firstCellIndex = row.getFirstCellNum();
						int lastCellIndex = row.getLastCellNum();
						for (int cellIndex = firstCellIndex; cellIndex < lastCellIndex; cellIndex++) {
							Cell cell = row.getCell(cellIndex);
							if (cell != null) {
								cont.append(cell.toString() + "\n");
							}
						}
					}
				}
			}

			content = cont.toString();
		} catch (Exception e) {
			logger.error("-->Disposable exception, no break-->");
			logger.error(e.getMessage(), e);
		} finally {
			if (workBook != null) {
				try {
					workBook.close();
				} catch (Exception e) {
					logger.error(e.getMessage(), e);
				}
			}
		}

		return content;
	}

	/** the pdf file filter. */
	private final String FILTER_PDF = ".pdf";
	/** the word doc file filter. */
	private final String FILTER_DOCX = ".docx";
	/** the word doc file filter. */
	private final String FILTER_DOC = ".doc";
	/** the excel xls file filter. */
	private final String FILTER_XLS = ".xls";
	/** the excel xlsx file filter. */
	private final String FILTER_XLSX = ".xlsx";

	/**
	 * get the file content.
	 *
	 * @param filepath the path of file.
	 * @return the content of file.
	 */
	public String getFileContent(String filepath) {
		String content = "";
		if (null != filepath && new File(filepath).exists()) {
			try {
				File file = new File(filepath);
				String filter = this.getFilterOfFile(filepath);
				// if the filter is null, we can't know how to open it or how to get the content
				// of it.
				if (filepath != null && file.exists() && null != filter) {
					InputStream inStream = new FileInputStream(filepath);
					try {
						if (inStream.available() > 0) {
							if (filter.toLowerCase().equals(".txt")) {
								content = this.getTxtContent(filepath);
							} else {
								content = this.getFileContent(filepath, inStream, filter);
							}
						}
					} finally {
						if (inStream != null) {
							inStream.close();
						}
					}

				}
			} catch (Exception e) {
				logger.error("-->Disposable exception, no break-->");
				logger.error("no break->" + e.getMessage(), e);
			}
		}

		return content;
	}

	/**
	 * get the file content.
	 *
	 * @param filepath the path of file.
	 * @return the content of file.
	 */
	public String getFileContent(String filepath, String coding) {
		String content = "";
		if (null != filepath) {
			try {
				File file = new File(filepath);
				if (file.exists()) {
					String filter = this.getFilterOfFile(filepath);
					// if the filter is null, we can't know how to open it or how to get the content
					// of it.
					if (filepath != null && file.exists() && null != filter) {
						InputStream inStream = new FileInputStream(filepath);
						try {
							if (inStream.available() > 0) {
								if (filter.toLowerCase().equals(".txt")) {
									content = this.getTxtContent(filepath);
								} else {
									content = this.getFileContent(filepath, inStream, filter);
								}
							}
							inStream.close();
						} finally {
							if (null != inStream) {
								inStream.close();
							}
						}

					}
				}
			} catch (Exception e) {
				logger.error("-->Disposable exception, no break-->");
				logger.error(e.getMessage(), e);
			}
		}
		return content;
	}

	/**
	 * get the file content.
	 *
	 * @param instream the input stream of file.
	 * @param filter   the filter of file.
	 * @return the content of file.
	 */
	public String getFileContent(InputStream instream, String filter) {
		String content = null;
		if (instream != null && filter != null && filter.startsWith(".")) {
			if (FILTER_DOC.equals(filter)) {
				// get the content of word doc.
				content = this.getWordDocContent(instream);
			} else if (FILTER_DOCX.equals(filter)) {
				// get the content of word docx.
				content = this.getWordDocxContent(instream);
			} else if (FILTER_PDF.equals(filter)) {
				// get the pad content.
				content = this.getPDFContent(instream);
			} else if (FILTER_XLS.equals(filter)) {
				content = this.getXLSContentPoi(instream, FILTER_XLS);
			} else if (FILTER_XLSX.equals(filter)) {
				content = this.getXLSContentPoi(instream, FILTER_XLSX);
			} else {
				// get the content of txt.
				content = this.getTxtContent(instream);
			}
		}
		return content;
	}

	/**
	 * get the file content.
	 *
	 * @param instream the input stream of file.
	 * @param filter   the filter of file.
	 * @return the content of file.
	 */
	private String getFileContent(String filePath, InputStream instream, String filter) {
		String content = null;
		if (instream != null && filter != null && filter.startsWith(".")) {
			if (FILTER_DOC.equals(filter)) {
				// get the content of word doc.
				content = this.getWordDocContent(instream);
			} else if (FILTER_DOCX.equals(filter)) {
				// get the content of word docx.
				content = this.getWordDocxContent(instream);
			} else if (FILTER_PDF.equals(filter)) {
				// get the pad content.
				content = this.getPDFContent(instream);
			} else if (FILTER_XLS.equals(filter)) {
				content = this.getXLSContentPoi(instream, FILTER_XLS);
			} else if (FILTER_XLSX.equals(filter)) {
				content = this.getXLSContentPoi(instream, FILTER_XLSX);
			}
		}
		return content;
	}

	/**
	 * get the filter of file.
	 *
	 * @param filepath the file path.
	 * @return the filter of file.
	 */
	public String getFilterOfFile(String filepath) {
		String filter = null;
		if (null == filepath || filepath.length() == 0) {
			return filter;
		}

		if (filepath.lastIndexOf(".") > 1) {
			filter = filepath.substring(filepath.lastIndexOf("."), filepath.length());
			filter = filter.toLowerCase();
		}

		return filter;
	}

	/**
	 * return the suffix of file. e.g.: a file called c:/test/test.jpg, the method
	 * would return .jpg.
	 *
	 * @param name the name or path of the file.
	 * @return
	 */
	public String getSuffix(String name) {
		VaipFileUtil fileUtil = new VaipFileUtil();
		return fileUtil.getSuffix(name);
	}
}
